package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.vo.ProductDeleteVo;
import com.ruoyi.fly.domain.vo.ProductIdsVo;
import com.ruoyi.fly.domain.vo.StoreProductVo;
import com.ruoyi.fly.mapper.StoreProductMapper;
import com.ruoyi.fly.mapper.StoreServerMapper;
import com.ruoyi.fly.service.StoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreProductServiceImpl implements StoreProductService {

    @Autowired
    private StoreProductMapper storeProductMapper;

    @Autowired
    private StoreServerMapper storeServerMapper;

    /**
     * 新增店铺商品
     *
     * @param storeProductVo
     * @return
     */
    @Override
    public R insertStoreProduct(StoreProductVo storeProductVo) {
        storeProductVo.setCreateTime(DateUtils.getNowDate());
        storeProductVo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return storeProductMapper.insertStoreProduct(storeProductVo) > 0 ? R.ok("新增成功") : R.fail("操作失败");
    }

    /**
     * 修改店铺商品
     *
     * @param storeProductVo
     * @return
     */
    @Override
    public R updateStoreProduct(StoreProductVo storeProductVo) {
        storeProductVo.setUpdateTime(DateUtils.getNowDate());
        storeProductVo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return storeProductMapper.updateStoreProduct(storeProductVo) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    /**
     * 删除店铺商品
     *
     * @param id
     * @return
     */
    @Override
    public R deleteStoreProductById(String id) {
        return storeProductMapper.deleteStoreProductById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteStoreProductByIds(String[] id) {
        return storeProductMapper.deleteStoreProductByIds(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    /**
     * 批量删除店铺下的商品或服务
     * @param productDeleteVo
     * @return
     */
    @Override
    public R deleteStoreProductByProductIds(ProductDeleteVo productDeleteVo) {
        List<ProductIdsVo> productIdsVoList = productDeleteVo.getProductIdsVoList();
        for (ProductIdsVo productIdsVo : productIdsVoList) {
            if (productIdsVo.getCategoryType() == 0){
                storeProductMapper.deleteStoreProductByProductId(productIdsVo.getId(),productDeleteVo.getStoreId());
            }else {
                storeServerMapper.deleteStoreServerByServerId(productIdsVo.getId(), productDeleteVo.getStoreId());
            }
        }
        return R.ok("删除成功");
    }
}
